Page({
    data: {
        price: '', //供应价格
        priceErr: false,
        purchase_price: '', //批发供应价
        purchase_priceErr: false,
        stock: '', //库存
        stockErr: false,
        info: null,
        buttonLoading: false
    },
    onLoad: function(options) {
        const eventChannel = this.getOpenerEventChannel()
        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
        eventChannel.on('acceptDataFromOpenerPage', ({ info }) => {
            this.setData({ info, price: info.cost_price, stock: info.sku, purchase_price: info.purchase_price })
        })
    },
    //验证价格
    verification(e) {
        const { field } = e.currentTarget.dataset
        const { value } = e.detail
        this.setData({
            [`${field}`]: this.formatter(value)
        })
    },
    /**验证金额输入 */
    formatter(value) {
        value = value.toString()
        value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
        value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
        if (value.indexOf(".") < 0 && value != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            value = parseFloat(value);
        }
        value = parseFloat(value)
        if (Number.isNaN(value)) return ''
        return value
    },
    //判断批发价格不能少于普通供应价格
    judgePrice() {
        const cost_price = Number(this.data.price)
        const wholesale_price = Number(this.data.purchase_price)
        if (wholesale_price > cost_price) {
            wx.showToast({
                title: '批发供应价不能大于普通供应价',
                icon: 'none',
                duration: 3000
            })
            this.setData({
                priceErr: true,
                purchase_priceErr: true
            })
            return false
        } else {
            return true
        }
    },
    //清除错误提示
    clearErr() {
        this.setData({ priceErr: false, stockErr: false, purchase_priceErr: false })
    },
    edit() {
        this.setData({ buttonLoading: true })
        this.delay(() => {
            const { info, price, stock, purchase_price } = this.data
            if (price === '') return this.setData({ priceErr: true, buttonLoading: false })
            if (info.supplier_type === 1) {
                if (purchase_price === '') return this.setData({ purchase_priceErr: true })
                if (!this.judgePrice()) return
            }

            if (stock === '') return this.setData({ stockErr: true, buttonLoading: false })

            let reqData = {
                product_id: info.product_id,
                cost_price: price,
                sku: stock
            }

            if (info.supplier_type === 1) {
                reqData.purchase_price = purchase_price
            }

            this.request({
                url: '/supplier/product/setSku',
                method: 'POST',
                data: reqData
            }, () => {
                wx.showToast({
                    title: '修改成功',
                    icon: 'success'
                })
                const eventChannel = this.getOpenerEventChannel()
                eventChannel.emit('refresh');

                this.delay(() => {
                    wx.navigateBack()
                })
            }, () => {
                this.setData({
                    buttonLoading: false
                })
            })
        }, 150)
    }
})